// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.ai.states;

import fmod.fmod.FMODManager;
import zombie.ai.State;
import zombie.audio.parameters.ParameterCharacterMovementSpeed;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoPlayer;
import zombie.characters.IsoZombie;
import zombie.core.skinnedmodel.advancedanimation.AnimEvent;
import zombie.util.Type;

public final class BumpedState extends State {
    private static final BumpedState _instance = new BumpedState();

    public static BumpedState instance() {
        return _instance;
    }

    @Override
    public void enter(IsoGameCharacter owner) {
        owner.setBumpDone(false);
        owner.setVariable("BumpFallAnimFinished", false);
        owner.getAnimationPlayer().setTargetToAngle();
        owner.getForwardDirection().setLengthAndDirection(owner.getAnimationPlayer().getAngle(), 1.0F);
        this.setCharacterBlockMovement(owner, true);
        if (owner.getVariableBoolean("BumpFall")) {
            long _long = owner.playSound("TripOverObstacle");
            ParameterCharacterMovementSpeed parameterCharacterMovementSpeed = ((IsoPlayer)owner).getParameterCharacterMovementSpeed();
            owner.getEmitter()
                .setParameterValue(_long, parameterCharacterMovementSpeed.getParameterDescription(), parameterCharacterMovementSpeed.calculateCurrentValue());
            String string = owner.getVariableString("TripObstacleType");
            if (string == null) {
                string = "zombie";
            }

            owner.clearVariable("TripObstacleType");
            byte byte0 = -1;
            switch (string.hashCode()) {
                case 3568542:
                    if (string.equals("tree")) {
                        byte0 = 0;
                    }
                default:
                    byte byte1 = switch (byte0) {
                        case 0 -> 5;
                        default -> 6;
                    };
                    owner.getEmitter().setParameterValue(_long, FMODManager.instance.getParameterDescription("TripObstacleType"), (float)byte1);
            }
        }
    }

    @Override
    public void execute(IsoGameCharacter owner) {
        boolean _boolean = owner.isBumpFall() || owner.isBumpStaggered();
        this.setCharacterBlockMovement(owner, _boolean);
    }

    private void setCharacterBlockMovement(IsoGameCharacter isoGameCharacter, boolean _boolean) {
        IsoPlayer isoPlayer = Type.tryCastTo(isoGameCharacter, IsoPlayer.class);
        if (isoPlayer != null) {
            isoPlayer.setBlockMovement(_boolean);
        }
    }

    @Override
    public void exit(IsoGameCharacter owner) {
        owner.clearVariable("BumpFallType");
        owner.clearVariable("BumpFallAnimFinished");
        owner.clearVariable("BumpAnimFinished");
        owner.setBumpType("");
        owner.setBumpedChr(null);
        IsoPlayer isoPlayer = Type.tryCastTo(owner, IsoPlayer.class);
        if (isoPlayer != null) {
            isoPlayer.setInitiateAttack(false);
            isoPlayer.attackStarted = false;
            isoPlayer.setAttackType(null);
        }

        if (isoPlayer != null && owner.isBumpFall()) {
            owner.fallenOnKnees();
        }

        owner.setOnFloor(false);
        owner.setBumpFall(false);
        this.setCharacterBlockMovement(owner, false);
        if (owner instanceof IsoZombie && ((IsoZombie)owner).target != null) {
            owner.pathToLocation((int)((IsoZombie)owner).target.getX(), (int)((IsoZombie)owner).target.getY(), (int)((IsoZombie)owner).target.getZ());
        }
    }

    @Override
    public void animEvent(IsoGameCharacter owner, AnimEvent event) {
        if (event.m_EventName.equalsIgnoreCase("FallOnFront")) {
            owner.setFallOnFront(Boolean.parseBoolean(event.m_ParameterValue));
            owner.setOnFloor(owner.isFallOnFront());
        }

        if (event.m_EventName.equalsIgnoreCase("FallOnBack")) {
            owner.setOnFloor(Boolean.parseBoolean(event.m_ParameterValue));
        }
    }
}
